--- title: "Intro to CZI files Part 2" date: 2020-03-22T09:54:08-07:00 draft: false tags: ["czi", "image analysis", tutorial", "jenna"] ---
Reading CZI files with Python.
Purpose: Jclub tutorial for how to process czi files in python. This notebook will just be how to open CZI files and view them in jupyter notebook.
Disclaimer: Marc and Ciera taught me everything I know
General info: The best image analysis tutorial everrrrr
### in the terminal use cd to get to the directory you want to be in.
### go into the conda environemnt where you installed czifile stuff
souce activate 'yourenv' # change 'yourenv' to whatever you named your environment
### Then type:
jupyter notebook
### this should open up a directory in your browser.
## Click new and then click on whichever environment you are in. This should open a new notebook!
### Essential for viewing importing and viewing images
import czifile #this is the package to import CZI files
import matplotlib.pyplot as plt #this package visualizes images
### Packages for manipulating images
from scipy import ndimage as ndi
from skimage import filters, measure, segmentation, transform, exposure, img_as_ubyte, feature, morphology
import os
ProjectDirectory = ('/Users/jennahaines/Box Sync/Eisen_Lab/IntrotoCZIfiles')
ProjectData = ('/Users/jennahaines/Box Sync/Eisen_Lab/IntrotoCZIfiles/data/')
ProjectBin = (ProjectDirectory + '/bin')
I like to make a new results notebook in the same directory for each jupyter notebook - that way I know which images came from which code later. I based it off of Nobel et al., 2009.
ProjectDirectory
NotebookResultsPath = (ProjectDirectory)
os.chdir(NotebookResultsPath)
I like to put the CZI name as a seperate variable so that I can easily hook things I am writing into loops
CziName = '100118-oligopaint3-2-02.czi'
I make a full path varaible in a second step so that I can loop through files easily
FullPath = (ProjectData + CziName)
print(FullPath)
import czifile
czi_array = czifile.imread(FullPath) #read the czi file.
print(czi_array.shape)
I use squeeze function to take out all of the unused elements
czi_array = czi_array.squeeze() #take out the dimensions that are not important
print(czi_array.shape)
Tip its 0 based meaning 0 is a channel too.
ChannelStack = czi_array[2,...]
print(ChannelStack.shape)
import matplotlib.pyplot as plt
%matplotlib inline
in this case I picked 15.. change the numbers around to go through the different images
plt.imshow(ChannelStack[15])
There may come a time (and that time might be right now) where you would like to see all the images in this stack at once instead of typing them out manually.
I wrote out a loop that does this.
fig, axs = plt.subplots(nrows = 4, ncols = 5, figsize=(50, 50))
for ax, pln in zip(axs.flat, range(ChannelStack.shape[0])) :
ax.imshow(ChannelStack[pln])
ax.axis('off')
ax.set_title(str(pln))
plt.tight_layout()
### To save this image as a png in your directory uncomment this line
plt.savefig('200323-StackImages.png')